package com.dss.auto_deploy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dss.auto_deploy.domain.AdmProject;
import com.dss.auto_deploy.domain.AdmUser;
import com.dss.auto_deploy.dto.AdmProjectAddDto;
import com.dss.auto_deploy.dto.AdmProjectPageDto;
import com.dss.auto_deploy.dto.AdmProjectUpdateDto;
import com.dss.auto_deploy.mapper.AdmProjectMapper;
import com.dss.auto_deploy.mapper.AdmUserMapper;
import com.dss.auto_deploy.service.IAdmProjectService;
import com.dss.auto_deploy.util.BeanUtils;
import com.dss.auto_deploy.util.CacheUtils;
import com.dss.auto_deploy.util.JacksonUtils;
import com.dss.auto_deploy.util.StringUtils;
import com.dss.auto_deploy.vo.AdmProjectVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Description:开发项目Service实现类
* @author dongshoushan
* @date 2022-09-22 14:39:06
*/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdmProjectServiceImpl  extends ServiceImpl<AdmProjectMapper,AdmProject> implements IAdmProjectService {

    /**
    *
    * 开发项目Mapper
    */
    private final AdmProjectMapper admProjectMapper;
    private final CacheUtils cacheUtils;
    private final AdmUserMapper admUserMapper;

    /**
    * 新增开发项目
    * @param admProjectAddDto
    * @return Long
    */
    @Override
    public int insert(AdmProjectAddDto admProjectAddDto){
        AdmProject admProject = BeanUtils.copy(admProjectAddDto, AdmProject.class);
        admProject.setCreateTime(new Date());
        admProject.setUpdateTime(new Date());
        int insert = admProjectMapper.insert(admProject);
        if (insert == 1) {
            log.info("新增成功{}", JacksonUtils.toString(admProject));
        }
        cacheUtils.deleteObject("getAllAdmProject");
        return insert;
    }


    /**
    * 修改开发项目
    * @param admProjectUpdateDto
    * @return Long
    */
    @Override
    public int update(AdmProjectUpdateDto admProjectUpdateDto){
        AdmProject admProject = BeanUtils.copy(admProjectUpdateDto, AdmProject.class);
        int number = admProjectMapper.updateById(admProject);
        if (number == 1) {
            log.info("修改成功{}",JacksonUtils.toString(admProject));
        }
        cacheUtils.deleteObject("getAllAdmProject");
        return number;
    }


    /**
    * 批量删除开发项目
    * @param ids
    * @return Long
    */
    @Override
    public void deleteBatch(Long[] ids){
        this.removeByIds(Arrays.asList(ids));
        cacheUtils.deleteObject("getAllAdmProject");
    }

    /**
    * 开发项目分页查询
    *
    * @param pageDto
    * @return List<AdmProject>
    */
    @Override
    public Page<AdmProjectVo> list(AdmProjectPageDto pageDto){
        Page<AdmProject> tablesPage = new Page<>(pageDto.getPage(), pageDto.getLimit());
        Page<AdmProject> pageSelectResult = admProjectMapper.selectPage(tablesPage, Wrappers.lambdaQuery(AdmProject.class)
                .like(StringUtils.isNotEmpty(pageDto.getContent()),AdmProject::getProjectName, pageDto.getContent())
                .orderByAsc(AdmProject::getSort));
        List<AdmProject> records = pageSelectResult.getRecords();
        List<AdmProjectVo> voList = convertToVo(records);
        Page<AdmProjectVo> page = new Page<>();
        page.setRecords(voList);
        page.setTotal(pageSelectResult.getTotal());
        page.setSize(pageSelectResult.getSize());
        return page;
    }

    @Override
    public List<AdmProjectVo> convertToVo(List<AdmProject> records) {
        if (records.isEmpty()){
            return new ArrayList<>();
        }
        List<AdmProjectVo> admProjectVos = BeanUtils.copyList(records, AdmProjectVo.class);
        List<AdmUser> userList = admUserMapper.selectList(null);
        admProjectVos.forEach(vo -> {
            if (StringUtils.isNotEmpty(vo.getTeamMembersMail())){
                List<String> mails = Arrays.asList(vo.getTeamMembersMail().split(","));
                List<AdmUser> admUsers = userList.stream().filter(s -> mails.contains(s.getMail())).collect(Collectors.toList());
                String collect = admUsers.stream().map(AdmUser::getRealName).collect(Collectors.joining(","));
                vo.setDeveloper(collect);
            }
        });
        return admProjectVos;
    }


    @Override
    public List<AdmProjectVo> getAll(String name) {
        List<AdmProject> admDockers = admProjectMapper.selectList(Wrappers.lambdaQuery(AdmProject.class)
                .like(StringUtils.isNotEmpty(name),AdmProject::getProjectName, name)
                .orderByAsc(AdmProject::getSort)
        );
        return convertToVo(admDockers);
    }

    @Override
    public List<AdmProject> getAll() {
        if (cacheUtils.hasKey("getAllAdmProject")) {
            return cacheUtils.getCacheObject("getAllAdmProject");
        }else {
            List<AdmProject> list = admProjectMapper.selectList(Wrappers.lambdaQuery(AdmProject.class)
                    .orderByAsc(AdmProject::getSort));
            cacheUtils.setCacheObject("getAllAdmProject", list,60*60*1000*24);
            return list;
        }
    }

}
